<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket client</title>
</head>
<body>
<input type="text" id="content" /><input type="button" id="send" value="发送">
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
    $(function () {
        var wsServer = 'ws://127.0.0.1:9501';
        var websocket = new WebSocket(wsServer);
        websocket.onopen = function (evt) {
            console.log("Connected to WebSocket server.");
        }

        websocket.onclose = function (evt) {
            console.log("Disconnected");
        }

        websocket.onmessage = function (evt) {
            console.log('Retrieved data from server: ' + evt.data);
        }

        websocket.onerror = function (evt, e) {
            console.log('Error occured: ' + evt.data);
        }

        $("#send").click(function () {
            var content = $("#content").val()
            websocket.send(content)
        })
    })
</script>
</html>